package org.example.myportfolio.dao.repository.impl;

import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.example.myportfolio.dao.repository.PageWorKCacheRepository;
import org.example.myportfolio.pojo.po.WorkPO;
import org.example.myportfolio.pojo.vo.WorkVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Repository;

import java.time.Duration;
import java.util.List;



@Slf4j
@Repository
public class PageWorKCacheRepositoryImpl implements PageWorKCacheRepository {


    @Autowired
    private RedisTemplate<String, PageInfo<WorkPO>> redisTemplate;

    @Override
    public PageInfo<WorkPO> pageWorkList(Integer pageNum, Integer pageSize) {
        ValueOperations<String,PageInfo<WorkPO>> operations = redisTemplate.opsForValue();
        String key = "WORK"+pageNum;
        try {
            PageInfo<WorkPO> workPOSList = operations.get(key);
            return workPOSList;
        } catch (Exception e) {
            e.printStackTrace();
            return PageInfo.emptyPageInfo();
        }
    }

    @Override
    public void saveWorkList(Integer pageNum, Integer pageSize,PageInfo<WorkPO> workVOS) {
        ValueOperations<String, PageInfo<WorkPO>> operations = redisTemplate.opsForValue();
        String key = "WORK"+pageNum;
        try {
            operations.set(key,workVOS, Duration.ofHours(1)); //设置缓存过期时间
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
